﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gert3D
{
  /// <summary>
  /// Stores all information about an intersection of a ray with a primitive.
  /// The information included are the world space position, the surface normal, 
  /// the texture coordinates and the T value of the intersection.
  /// </summary>
  public class IntersectionInfo
  {
    private Vector3 _intersectionPoint = null;
    private Vector3 _normal = null;
    private Vector3 _texCoords = null;
    private Vector3 _barycentric = null;
    private double _t = 0f;

    /// <summary>
    /// Creates a new intersection info with no information in it.
    /// </summary>
    public IntersectionInfo()
    {
    }

    /// <summary>
    /// Gets or sets the point of intersection in world space coordinates.
    /// </summary>
    public Vector3 IntersectionPoint
    {
      get { return _intersectionPoint; }
      set { _intersectionPoint = value; }
    }

    /// <summary>
    /// Gets the surface normal at the intersection point.
    /// </summary>
    public Vector3 Normal
    {
      get { return _normal; }
      set { _normal = value; }
    }

    /// <summary>
    /// Gets the texture coordinates at the intersection point.
    /// </summary>
    public Vector3 TexCoords
    {
      get { return _texCoords; }
      set { _texCoords = value; }
    }

    /// <summary>
    /// Gets the t value of the intersection. It can be used to effectively 
    /// check if there already was an intersection that was closer to the ray origin.
    /// It can also be used in the ray formular (p = origin + direction * t)
    /// to calculate the point of intersection.
    /// </summary>
    public double T
    {
      get { return _t; }
      set { _t = value; }
    }

    public Vector3 Barycentric
    {
      get { return _barycentric; }
      set { _barycentric = value; }
    }

    public override string ToString()
    {
      string inter;
      if (_intersectionPoint != null)
        inter = "I" + _intersectionPoint;
      else
        inter = "I()";

      string normal;
      if (_normal != null)
        normal = "N" + _normal;
      else
        normal = "N()";

      string tex;
      if (_texCoords != null)
        tex = "T" + _texCoords;
      else
        tex = "T()";

      string t;
      if (_t > 0.0f)
        t = "t(" + _t + ")";
      else
        t = "t()";

      return inter + "," + normal + "," + tex + "," + t;
    }

  }
}
